package com.doctor.windflower_doctor.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.activity.AboutUsActivity;
import com.doctor.windflower_doctor.activity.FeedBackActivity;
import com.doctor.windflower_doctor.activity.MainActivity;
import com.doctor.windflower_doctor.adapter.NavDrawerListAdapter;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.entity.NavDrawerItem;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MenuFragment extends BaseFragment implements OnItemClickListener, OnClickListener {

	private ListView mDrawerList;
	private String[] mNavMenuTitles;
	private TypedArray mNavMenuIconsTypeArray;
	private ArrayList<NavDrawerItem> mNavDrawerItems;
	private NavDrawerListAdapter mAdapter;
	private SLMenuListOnItemClickListener mCallback;
	private int selected = -1;
	private ImageView user_icon;
	private TextView question_replay, about_us, name;
	private RelativeLayout person_icon;
	private DisplayImageOptions options;
	public static final String ACTION_FRAGMENT_MYQUESTIONFRAGMENT = "com.doctor.windflower_doctor.fragment.MyQueationFragment";
	public static final String ACTION_FRAGMENT_MYQUESTIONHALLFRAGMENT = "com.doctor.windflower_doctor.fragment.MyQuestionHallFragment";
	public static final String ACTION_FRAGMENT_USERFRAGMENT = "com.doctor.windflower_doctor.activity_UserFragment";
	public static final String ACTION_FRAGMENT = "com.doctor.windflower_doctor.activity";

	@Override
	public void onAttach(Activity activity) {
		try {
			mCallback = (SLMenuListOnItemClickListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString() + " must implement OnResolveTelsCompletedListener");
		}
		IntentFilter filter = new IntentFilter();
		filter.addAction(ACTION_FRAGMENT_MYQUESTIONFRAGMENT);
		filter.addAction(ACTION_FRAGMENT_MYQUESTIONHALLFRAGMENT);
		filter.addAction(ACTION_FRAGMENT_USERFRAGMENT);
		getActivity().registerReceiver(receiver, filter);
		super.onAttach(activity);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// update selected item and title, then close the drawer
		mDrawerList.setItemChecked(position, true);
		mDrawerList.setSelection(position);

		if (mCallback != null) {
			mCallback.selectItem(position, mNavMenuTitles[position]);
		}
		selected = position;
	}

	/**
	 * 左侧菜单 点击回调接口
	 * 
	 * @author FX_SKY
	 * 
	 */
	public interface SLMenuListOnItemClickListener {

		public void selectItem(int position, String title);

		public void show();
	}

	@Override
	protected View setConentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_menu, container, false);

	}

	@Override
	protected void getSaveData(Bundle savedInstanceState) {

		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {

		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		mDrawerList.setOnItemClickListener(this);
		person_icon.setOnClickListener(this);
		question_replay.setOnClickListener(this);
		about_us.setOnClickListener(this);
	}

	@Override
	protected void findViews(View view) {
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.loading_img).showImageOnFail(R.drawable.list_default_icon)
				.showImageForEmptyUri(R.drawable.list_default_icon).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.ARGB_8888).build();
		mDrawerList = (ListView) view.findViewById(R.id.left_menu);
		user_icon = (ImageView) view.findViewById(R.id.user_icon);
		ImageLoader.getInstance().displayImage(SharedPreferencesDB.getInstance(getActivity()).getImage(), user_icon, options);
		person_icon = (RelativeLayout) view.findViewById(R.id.person_icon);
		question_replay = (TextView) view.findViewById(R.id.question_replay);
		about_us = (TextView) view.findViewById(R.id.about_us);
		name = (TextView) view.findViewById(R.id.name);
		name.setText(SharedPreferencesDB.getInstance(getActivity()).getAccountName());
		mNavMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
		// nav drawer icons from resources
		mNavMenuIconsTypeArray = getResources().obtainTypedArray(R.array.nav_drawer_icons);

		mNavDrawerItems = new ArrayList<NavDrawerItem>();

		// adding nav drawer items to array
		// Home
		mNavDrawerItems.add(new NavDrawerItem(mNavMenuTitles[0], mNavMenuIconsTypeArray.getResourceId(0, -1)));
		// Find People
		mNavDrawerItems.add(new NavDrawerItem(mNavMenuTitles[1], mNavMenuIconsTypeArray.getResourceId(1, -1)));
		// Photos
		mNavDrawerItems.add(new NavDrawerItem(mNavMenuTitles[2], mNavMenuIconsTypeArray.getResourceId(2, -1)));
		// Communities, Will add a counter here
		// mNavDrawerItems.add(new NavDrawerItem(mNavMenuTitles[3],
		// mNavMenuIconsTypeArray.getResourceId(3, -1), true, "22"));
		// Pages
		mNavDrawerItems.add(new NavDrawerItem(mNavMenuTitles[3], mNavMenuIconsTypeArray.getResourceId(3, -1)));
		// What's hot, We will add a counter here
		// mNavDrawerItems.add(new NavDrawerItem(mNavMenuTitles[5],
		// mNavMenuIconsTypeArray.getResourceId(5, -1), true, "50+"));

		// Recycle the typed array
		mNavMenuIconsTypeArray.recycle();

		// setting the nav drawer list adapter
		mAdapter = new NavDrawerListAdapter(getActivity(), mNavDrawerItems);
		mDrawerList.setAdapter(mAdapter);

		if (selected != -1) {
			mDrawerList.setItemChecked(selected, true);
			mDrawerList.setSelection(selected);
		} else {
			mDrawerList.setItemChecked(0, true);
			mDrawerList.setSelection(0);
		}

	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.person_icon:
			// mDrawerList.setItemChecked(3, true);
			// mDrawerList.setSelection(3);
			if (mCallback != null) {
				mCallback.selectItem(3, mNavMenuTitles[3]);
				mDrawerList.clearChoices();
			}
			break;
		case R.id.question_replay:
			intent = new Intent(getActivity(), FeedBackActivity.class);
			startActivity(intent);
			break;
		case R.id.about_us:
			intent = new Intent(getActivity(), AboutUsActivity.class);
			startActivity(intent);
			// if (mCallback != null) {
			// mCallback.show();
			// }
			break;
		}
	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		getActivity().unregisterReceiver(receiver);
		super.onDestroyView();
	}

	public BroadcastReceiver receiver = new MyBroadcast();

	public class MyBroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			// TODO Auto-generated method stub
			String action = arg1.getAction();
			if (action.equals(ACTION_FRAGMENT_MYQUESTIONFRAGMENT)) {
				if (MainActivity.instance != null)
					MainActivity.instance.setType(arg1.getIntExtra("type", 0));
				mDrawerList.setItemChecked(0, true);
				mDrawerList.setSelection(0);
				selected = 0;
			} else if (action.equals(ACTION_FRAGMENT_USERFRAGMENT)) {
				mDrawerList.setItemChecked(3, true);
				mDrawerList.setSelection(3);
				selected = 3;
			} else if (action.equals(ACTION_FRAGMENT_MYQUESTIONHALLFRAGMENT)) {
				mDrawerList.setItemChecked(1, true);
				mDrawerList.setSelection(1);
				selected = 1;
			}
			if (mCallback != null) {
				mCallback.selectItem(selected, mNavMenuTitles[selected]);
			}
		}
	}

}
